home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 August / Macworld (1999-08).dmg / Shareware World / Info / For Developers / MADE 1.4.0 / Essentials / Essential Menus.c < prev    next >
Text File  |  1999-05-26  |  6KB  |  235 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /*                                                                   */
  3. /*        MADE - Macintosh Application Development Essentials        */
  4. /*        ---------------------------------------------------        */
  5. /*           (c) Sig Software, http://www.sigsoftware.com/           */
  6. /*                                                                   */
  7. /* These files can only be used for experimental purposes. To obtain */
  8. /* fully commented code, source code for the functions in Essential  */
  9. /*   Extras.h and permission for usage in final projects, you must   */
  10. /*    purchase a license. See documentation for more information.    */
  11. /*                                                                   */
  12. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  13. /*                                                                   */
  14. /*  Essential Menus.c                                                */
  15. /*  -----------------                                                */
  16. /*                                                                   */
  17. /*  Menu handling, Drag Manager and cursor routines.                 */
  18. /*                                                                   */
  19. /*  Version 1.0.0 - 10th November 1996                               */
  20. /*  Version 1.0.1 - 4th June 1997 - Resets port in DragTrackHandler  */
  21. /*  Version 1.1.0 - 29th January 1998 - New OS function names        */
  22. /*  Version 1.2.0 - 20th November 1998 - Reworked dragging code      */
  23. /*  Version 1.4.0 - 26th May 1999 - Hide errors during drags         */
  24. /*                                                                   */
  25. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  26.  
  27. #include "Essential Headers.h"
  28. #include "Essential Prototypes.h"
  29.  
  30. #if Use_Drag_Manager
  31.  
  32. #include <Drag.h>
  33.  
  34. Boolean                    hasDragManager=false;
  35.  
  36. // No support is provided here for data send procedures, but these can be added if needed
  37.  
  38. pascal OSErr            DragTrackHandler(DragTrackingMessage, WindowPtr, void *, DragReference);
  39. pascal OSErr            DragReceivHandler(WindowPtr, void*, DragReference);
  40. DragTrackingHandlerUPP    DragTrackUPP;
  41. DragReceiveHandlerUPP    DragReceivUPP;
  42.  
  43. #endif
  44.  
  45. // Menu item switching is based on the fact that the upper 16 bits of the value returned by
  46. // the system are the menu's ID and the lower 16 bits are the item number chosen in that menu.
  47. // Just remember these are IDs defined within the MENU resource, not the resource ID itself.
  48.  
  49. enum {
  50.     AppleStart=128*65536,
  51.     AppleAbout,
  52.     AppleSep1,
  53.     AppleItems,
  54.     AppleEnd=128*65536+65535
  55. };
  56.  
  57. Error InitMenuBar()
  58. {
  59.     Error        error=0;
  60.     Handle        theMenuBar;
  61.     MenuHandle    theAppleMenu;
  62.     
  63.     theMenuBar=GetNewMBar(128);
  64.     error=TestResError(theMenuBar);
  65.         _i(error)
  66.     
  67.     SetMenuBar(theMenuBar);
  68.     DisposeHandle(theMenuBar);
  69.     
  70.     theAppleMenu=GetMenuHandle(128);
  71.     AppendResMenu(theAppleMenu, 'DRVR');
  72.     
  73.     DrawMenuBar();
  74.     
  75.     _e
  76.     return error;
  77. }
  78.  
  79. void SelectMenuItem(long selection)
  80. {
  81.     Str255    menuItemName;
  82.     
  83.     if (selection>=AppleItems && selection<=AppleEnd) {
  84.         GetMenuItemText(GetMenuHandle(128), (short)(selection-AppleStart), menuItemName);
  85.         OpenDeskAcc(menuItemName);
  86.     } else
  87.         MyPerformMenu(selection);
  88.     
  89.     HiliteMenu(0);
  90. }
  91.  
  92. void SetMenuItemEnabled(short menuID, short menuItem, Boolean enable)
  93. {
  94.     MenuHandle    menuHandle;
  95.     
  96.     menuHandle=GetMenuHandle(menuID);
  97.     
  98.     if (enable)
  99.         EnableItem(menuHandle, menuItem);
  100.     else
  101.         DisableItem(menuHandle, menuItem);
  102. }
  103.  
  104. void ShowResCursor(short cursorID)
  105. {
  106.     CursPtr    theCursor;
  107.     
  108.     theCursor=*GetCursor(cursorID);
  109.     SetCursor(theCursor);
  110. }
  111.  
  112. #if Use_Drag_Manager
  113.     
  114. // In this model, a single Track and Receive handler is used for all drags. You can make
  115. // separate ones for each window. Also, the refCon parameter is left at zero here.
  116.  
  117. Error InitDragManager()
  118. {
  119.     Error    error=0;
  120.     
  121.     hasDragManager=true;
  122.     
  123.     DragTrackUPP=NewDragTrackingHandlerProc(DragTrackHandler);
  124.     error=InstallTrackingHandler(DragTrackUPP, 0, 0);
  125.     TestError(error);
  126.         _i(error);
  127.     
  128.     DragReceivUPP=NewDragReceiveHandlerProc(DragReceivHandler);
  129.     error=InstallReceiveHandler(DragReceivUPP, 0, 0);
  130.     TestError(error);
  131.     
  132.     _e
  133.     return error;
  134. }
  135.  
  136. pascal OSErr DragTrackHandler(DragTrackingMessage message, WindowPtr inWindow,
  137.     void* refCon, DragReference dragRef)
  138. {
  139.     Point            localPoint;
  140.     DragAttributes    attributes;
  141.     RgnHandle        region;
  142.     GrafPtr            oldPort;
  143.     RGBColor        backColor;
  144.     Error            error=0;
  145.     
  146.     if (HideErrors(&error)) {
  147.     
  148.         error=GetDragAttributes(dragRef, &attributes);
  149.             _i(error)
  150.         GetPort(&oldPort);
  151.         SetPort(inWindow);
  152.         GetBackColor(&backColor);
  153.         BackColor(whiteColor);
  154.                 
  155.         switch (message) {
  156.             case kDragTrackingInWindow:
  157.                 if (attributes&kDragHasLeftSenderWindow) {
  158.  
  159.                     error=GetDragMouse(dragRef, &localPoint, 0);
  160.                         _i(error)
  161.                     GlobalToLocal(&localPoint);
  162.                     
  163.                     region=MyGetDragRegion(inWindow, localPoint, dragRef);
  164.                     
  165.                     if (region) {
  166.                         ShowDragHilite(dragRef, region, true);
  167.                         DisposeRgn(region);
  168.                     } else
  169.                         HideDragHilite(dragRef);
  170.                 }
  171.                 break;
  172.                 
  173.              case kDragTrackingLeaveWindow:
  174.                 HideDragHilite(dragRef);
  175.                 break;
  176.         }
  177.  
  178.         RGBBackColor(&backColor);
  179.         SetPort(oldPort);
  180.     
  181.         _e
  182.         ShowErrors();
  183.     }
  184.     
  185.     return error;
  186. }
  187.  
  188. pascal OSErr DragReceivHandler(WindowPtr inWindow, void* refCon, DragReference dragRef)
  189. {
  190.     Error        error=0;
  191.     WindowPtr    oldPort;
  192.     Point        localPoint;
  193.     RGBColor    backColor;
  194.     
  195.     if (HideErrors(&error)) {
  196.         error=GetDragMouse(dragRef, &localPoint, 0);
  197.             _i(error)
  198.             
  199.         GetPort(&oldPort);
  200.         SetPort(inWindow);
  201.         
  202.         GetBackColor(&backColor);
  203.         BackColor(whiteColor);
  204.         HideDragHilite(dragRef);
  205.         RGBBackColor(&backColor);
  206.  
  207.         GlobalToLocal(&localPoint);
  208.         error=MyReceiveDrag(inWindow, localPoint, dragRef);
  209.         SetPort(oldPort);
  210.     
  211.         _e
  212.         ShowErrors();
  213.     }
  214.     
  215.     return error;
  216. }
  217.  
  218. void CreateDragRegion(RgnHandle region)
  219. {
  220.     RgnHandle    subtractRgn;
  221.     Point        dummyPoint;
  222.     
  223.     subtractRgn=NewRgn();
  224.     CopyRgn(region, subtractRgn);
  225.     InsetRgn(subtractRgn, 1, 1);
  226.     DiffRgn(region, subtractRgn, region);
  227.     DisposeRgn(subtractRgn);
  228.     
  229.     dummyPoint.h=dummyPoint.v=0;
  230.     LocalToGlobal(&dummyPoint);
  231.     OffsetRgn(region, dummyPoint.h, dummyPoint.v);
  232. }
  233.         
  234. #endif
  235.